package com.ruoyi.project.system.inspectreport.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uniquekey.IdGererateFactory;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.inspectreport.domain.InspectReport;
import com.ruoyi.project.system.inspectreport.service.IInspectReportService;
import com.ruoyi.project.system.relevantimg.domain.RelevantImg;
import com.ruoyi.project.system.relevantimg.service.IRelevantImgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检验报告Controller
 * 
 * @author LCL
 * @date 2020-12-11
 */
@Controller
@RequestMapping("/system/inspectreport")
public class InspectReportController extends BaseController
{
    private String prefix = "system/inspectreport";

    @Autowired
    private IInspectReportService inspectReportService;
    @Autowired
    private IdGererateFactory idGererateFactory;
    @Autowired
    private IRelevantImgService relevantImgService;

    @RequiresPermissions("system:inspectreport:view")
    @GetMapping()
    public String inspectreport()
    {
        return prefix + "/inspectreport";
    }

    /**
     * 查询检验报告列表
     */
    @RequiresPermissions("system:inspectreport:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InspectReport inspectReport)
    {
        startPage();
        List<InspectReport> list = inspectReportService.selectInspectReportList(inspectReport);
        return getDataTable(list);
    }

    /**
     * 导出检验报告列表
     */
    @RequiresPermissions("system:inspectreport:export")
    @Log(title = "检验报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InspectReport inspectReport)
    {
        List<InspectReport> list = inspectReportService.selectInspectReportList(inspectReport);
        ExcelUtil<InspectReport> util = new ExcelUtil<InspectReport>(InspectReport.class);
        return util.exportExcel(list, "inspectreport");
    }

    /**
     * 新增检验报告
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存检验报告
     */
    @RequiresPermissions("system:inspectreport:add")
    @Log(title = "检验报告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InspectReport inspectReport,String[] imgBannerUrl)
    {
        inspectReport.setId(this.idGererateFactory.nextStringId());
        inspectReport.setName(inspectReport.getName());
        inspectReport.setTitle(inspectReport.getTitle());
        inspectReport.setNotes(inspectReport.getNotes());
        inspectReport.setCreated(DateUtils.getNowDate());
        inspectReport.setImg1Url(inspectReport.getImg1Url());
        inspectReport.setStatus(inspectReport.getStatus());
        inspectReport.setSort(inspectReport.getSort());
        inspectReportService.insertInspectReport(inspectReport);

        if(imgBannerUrl!=null){
            //检验报告图
            for (int i = 0; i < imgBannerUrl.length; i++) {
                RelevantImg relevantImg = new RelevantImg();
                relevantImg.setId(idGererateFactory.nextStringId());
                relevantImg.setImgUrl(imgBannerUrl[i]);
                relevantImg.setRelId(inspectReport.getId());
                relevantImg.setType(1);
                relevantImg.setStatus(0);
                relevantImg.setSort(Integer.toUnsignedLong(i));
                this.relevantImgService.insertRelevantImg(relevantImg);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改检验报告
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        InspectReport inspectReport = inspectReportService.selectInspectReportById(id);
        mmap.put("relevantImg",this.relevantImgService.findListByRelIdAndType(id,1));
        mmap.put("inspectReport", inspectReport);
        return prefix + "/edit";
    }

    /**
     * 修改保存检验报告
     */
    @RequiresPermissions("system:inspectreport:edit")
    @Log(title = "检验报告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InspectReport inspectReport,String[] imgBannerUrl)
    {
        //删除数据库原本的相册资源
        this.relevantImgService.deleteByRelId(inspectReport.getId());

        if(imgBannerUrl!=null){
            //检验报告图
            for (int i = 0; i < imgBannerUrl.length; i++) {
                RelevantImg relevantImg = new RelevantImg();
                relevantImg.setId(idGererateFactory.nextStringId());
                relevantImg.setImgUrl(imgBannerUrl[i]);
                relevantImg.setRelId(inspectReport.getId());
                relevantImg.setType(1);
                relevantImg.setStatus(0);
                relevantImg.setSort(Integer.toUnsignedLong(i));
                this.relevantImgService.insertRelevantImg(relevantImg);
            }
        }
        return toAjax(inspectReportService.updateInspectReport(inspectReport));
    }

    /**
     * 删除检验报告
     */
    @RequiresPermissions("system:inspectreport:remove")
    @Log(title = "检验报告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(inspectReportService.deleteInspectReportByIds(ids));
    }
}
